<?php
namespace hc;
class pageajax{
	public $firstRow; // 起始行数
	public $nowPage;
  public $listRows; // 列表每页显示行数
  public $parameter; // 分页跳转时要带的参数
  public $totalRows; // 总行数
  public $totalPages; // 分页总页面数
  public $rollPage=6;// 分页栏每页显示的页数
	public $html; //返回的分页代码
	public $url;//连接地址
	public $p='page';
	public $ajax_func='ajaxpage';

	// 分页显示定制
  private $config  = array(
      'a_head' => '1...',
      'a_pre'  => '<<',
      'a_nex'  => '>>',
      'a_end'  => '',
      'a_total'=>'',
      'theme'  => '%PRE% %HEAD% %LINK_PAGE% %NEX% %END% %TOTAL%',
  );


	public function init($listRows,$totalRows){
		$this->listRows=$listRows;
		$this->totalRows=$totalRows;
		$this->nowPage=empty($_REQUEST[$this->p])?1:intval($_REQUEST[$this->p]);
		$this->firstRow= $this->listRows * ($this->nowPage - 1);
		$this->totalPages =ceil($this->totalRows/$this->listRows); //总页数
		$this->setConfig('a_end',$this->totalPages);
		$this->setConfig('a_total','<span class="total">共'.$this->totalRows.'条记录</span>');
	}

	//分页配置
	 public function setConfig($name,$value) {
        if(isset($this->config[$name])) {
            $this->config[$name] = $value;
        }
    }
    public function var_p($p){
		$this->p=!empty($p)?$p:$this->p;
	}
	//url路径设置
	public function var_url($url=''){
		if(empty($url)){
			exit('连接url地址未定义');
		}
		$this->url=$url;
	}
	//当前页 及其他业显示
	public function show_page(){
		$p_pre='';
		$p_head='';
		$link_page = "";
		$p_end='';
		$p_nex='';
		$now_cool_page=$this->rollPage/2;
		$now_cool_page_ceil = ceil($now_cool_page);
		//上一页且总页数要大于滚动页数
		if($this->nowPage>1 && $this->totalPages>$this->rollPage){
			$ajax_pre="javascript:".$this->ajax_func."(".($this->nowPage-1).")";
			$p_pre='<a class="pre" href="'.$ajax_pre.'">'.$this->config['a_pre'].'</a>';
			//$p_pre='<a class="pre" href="'.$this->url.($this->nowPage-1).'">'.$this->config['a_pre'].'</a>';
		}
		//首页且当前页数要大于滚动页数
		if($this->totalPages > $this->rollPage && ($this->nowPage - $now_cool_page) >= 1){
			$ajax_head="javascript:".$this->ajax_func."(1)";
			$p_head='<a class="head" href="'.$ajax_head.'">'.$this->config['a_head'].'</a>';
		}

		//页码

        for($i = 1; $i <= $this->rollPage; $i++){
			if(($this->nowPage - $now_cool_page) <= 0 ){
				$page = $i;
			}elseif(($this->nowPage + $now_cool_page - 1) >= $this->totalPages){
				$page = $this->totalPages - $this->rollPage + $i;
			}else{
				$page = $this->nowPage - $now_cool_page_ceil + $i;
			}
            if($page > 0 && $page != $this->nowPage){

                if($page <= $this->totalPages){
                	$ajax_num="javascript:".$this->ajax_func."(".$page.")";
                    $link_page .= '<a class="num" href="' . $ajax_num.'">' . $page . '</a>';
                }else{
                    break;
                }
            }else{
                if($page > 0 && $this->totalPages>1){

                    $link_page .= '<a class="num current">' . $page . '</a>';
                }
            }
        }


		//尾页且当前页数要大于滚动页数
		if($this->totalPages > $this->rollPage && ($this->nowPage + $now_cool_page) < $this->totalPages){
			$ajax_end="javascript:".$this->ajax_func."(".$this->totalPages.")";
			$p_end='<a class="end" href="'.$ajax_end.'">...'.$this->config['a_end'].'</a>';
		}
		//下一页且总页数要大于滚动页数
		if($this->nowPage<$this->totalPages && $this->totalPages>$this->rollPage){
			$ajax_nex="javascript:".$this->ajax_func."(".($this->nowPage+1).")";
			$p_nex='<a class="nex" href="'.$ajax_nex.'">'.$this->config['a_nex'].'</a>';
		}
		//统计

		$ajax_html="<script></script>";
		//替换分页内容
		 //'theme'  => '%HEAD% %PRE% %LINK_PAGE% %NEX% %END%',
        $page_str = str_replace(
            array('%HEAD%','%PRE%','%LINK_PAGE%','%NEX%','%END%','%TOTAL%'),
            array($p_head,$p_pre,$link_page,$p_end,$p_nex,$this->config['a_total']),
            $this->config['theme']);
		return $page_str;
	}
}
?>
